
var MK ={
		
	tabs : function(options){

		var set = {
			tabs : $("#tabs-content > div"),
			nav : $("#tabs-nav > li > a"),
			delay: 5000,
			auto: false,
			enableHistory: false,
			effect: 'normal',
			duration: 200	
		}; 
		
		$.extend(set, options); 
		
		var current = set.tabs.first();
		var tabsWrapper = $("#tabs-content > div").parent();
		var newCurrent;
		var active = set.nav.first().parent();
	 	var hash;
		var isAnimating = false;
		

		var init = function(){
			set.tabs.hide();
			if(set.enableHistory){
				locationHash = window.location.hash;
				current = $(locationHash);	
				set.nav.each(function(){
					if ($(this).attr('href') === locationHash) {
						
						active = $(this).parent();
					}
				});				
			}		
			current.show();
			active.addClass('active');			
			switchTab();

			if(set.auto){
				autoRun();
			}			
		};

		var animation = function(){
			var animationEnd = function(){
				isAnimating = false;
				
			};
			var effects = {
				normal: function(){
					current.hide();
					newCurrent.show();
					animationEnd();	
				},
				fade: function(){
					current.fadeOut(set.duration, function(){
						newCurrent.fadeIn(set.duration, animationEnd);	
					});
				},
				slide: function(){
					current.slideUp(set.duration, function(){
						newCurrent.slideDown(set.duration, animationEnd);	
					});
				},
				slideX: function(){//TODO: dokonczyc efekt slidu na boki
					tabsWrapper.css("position","relative");
					var width = tabsWrapper.width();
					set.tabs.css('width',width);
					current.animate({'margin-left':-width - 20}, set.duration);
					newCurrent.css({
						'margin-left': width + 20
						
					}).show();
					newCurrent.animate({'margin-left':0}, set.duration, animationEnd);
				}
			};
			effects[set.effect]();
		};
		
		
		var testId = function(id){
			var test = false;	
			set.tabs.each(function(){
				if($(this).attr('id') === id){					
					test = true;
				}
			});
			return test;
		};
		
		var autoRun = function(){
			var interval;
			set.tabs.first().parent().parent().hover(function(){
				clearInterval(interval);
			},function(){
				interval = setInterval(function(){
				return nextTab();
			}, set.delay);
			});
		 	interval = setInterval(function(){
				return nextTab();
			}, set.delay);	
		};
		
		var setHash = function(){
			hash = active.children('a').attr('href');
			hash = hash.replace('#','');
		};

				
		var switchTab = function(id){
			if(id){
				
				if (testId(id) === true) {
					current = $("#" + id);
					
					set.nav.each(function(){
						if ($(this).attr('href') === "#" + id) {
							active = $(this).parent();
							$(this).trigger('click');
						}
					});	
				}else{
					return false;
				}	
			}else{
				set.nav.click(function(e){
					e.preventDefault();
					if($(this).parent().hasClass('active') || isAnimating === true){
						return;
					}					
					var target = $(this).attr('href');
					newCurrent = $(target);
					active = $(this).parent();					
					if(set.enableHistory === true){
						setHash();
						window.location.hash = hash;					
					}
					useLinkTarget = true;
					isAnimating = true;
					animation();
					current = newCurrent;
					set.nav.parent().removeClass('active');
					active.addClass('active');	
				});
			}
		};
		
		
		var nextTab = function(){
			if(current.next().length > 0){
		 		newCurrent = current.next();
				active = active.next();					
			}else {
				newCurrent = set.tabs.first();
				active = set.nav.first().parent();		
			}
	
			active.children('a').trigger('click');
			
		};
		
		var prevTab = function(){
			if (current.prev().length > 0) {
		  		newCurrent = current.prev();
		  		active = active.prev();
	  		}else {
				newCurrent = set.tabs.last();
				active = set.nav.last().parent();		
			}
		  	active.children('a').trigger('click');
		};
		
		var getCurrent = function(){
			return current;
		};
			
		init();	
		
		//deklaracja publicznego obiektu 	
		var _public = {};
		//powiazanie metod z publicznym obiektem "that" przez to staja sie publiczne 
		_public.switchTabs = switchTab;
		_public.nextTab = nextTab;
		_public.prevTab = prevTab;
		_public.getCurrent = getCurrent;
		
		return _public;
	}	
	
};
